Skip to content

Conversation

@sydney-runkle
Copy link
Collaborator

@sydney-runkle sydney-runkle commented Oct 27, 2025

Adding agent_name to AgentRuntime so that you can access more execution metadata on ModelRequest.

Likely to expand AgentRuntime in the future, but keeping semi-private for now.

"""Script demonstrating wrap_model_call middleware showing agent name for main and subagent calls."""

from langchain.agents import create_agent
from langchain.agents.middleware import wrap_model_call
from langchain_core.messages import HumanMessage
from langchain_core.tools import tool
from langgraph.config import get_config

# This will print the agent name for every model call (main or subagent)
@wrap_model_call
def print_agent_name(request, handler):
    """Middleware that prints the runtime agent_name and passes through the request."""
    print(f"[middleware] agent_name: {request.runtime.agent_name}")
    return handler(request)

# Define a subagent that will be called as a tool
subagent = create_agent(
    model="openai:gpt-4o-mini",
    middleware=[print_agent_name],
    name="subagent_graph"
)

# Define a tool that invokes the subagent
@tool
def trigger_subagent(query: str) -> str:
    """Call the subagent with the query."""
    response = subagent.invoke({"messages": [HumanMessage(content=query)]})
    # Return only the assistant's most recent message content
    return response["messages"][-1].content

# Main agent, which has access to the above tool
agent = create_agent(
    model="openai:gpt-4o-mini",
    tools=[trigger_subagent],
    middleware=[print_agent_name],
    name="sydney_graph"
)

# Compose a user message that requires the agent to call the subagent tool
result = agent.invoke({"messages": [HumanMessage(content="Use the trigger_subagent tool to answer: What's 2+2?")]})

print(result["messages"][-1].content)

# Example output:
# [middleware] agent_name: sydney_graph
# [middleware] agent_name: subagent_graph
# [middleware] agent_name: sydney_graph
# 2 + 2 equals 4.

@github-actions github-actions bot added langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0 feature labels Oct 27, 2025
@github-actions github-actions bot added feature and removed feature labels Oct 27, 2025
@eyurtsev
Copy link
Collaborator

OK this looks good. will we be able to add agent_name into

@dataclass()
class ToolCallRequest:
    """Tool execution request passed to tool call interceptors.

    Attributes:
        tool_call: Tool call dict with name, args, and id from model output.
        tool: BaseTool instance to be invoked, or None if tool is not
            registered with the `ToolNode`. When tool is `None`, interceptors can
            handle the request without validation. If the interceptor calls `execute()`,
            validation will occur and raise an error for unregistered tools.
        state: Agent state (`dict`, `list`, or `BaseModel`).
        runtime: LangGraph runtime context (optional, `None` if outside graph).
    """

    tool_call: ToolCall
    tool: BaseTool | None
    state: Any
    runtime: ToolRuntime

ToolRuntime if we're using it from within an agent?

@sydney-runkle sydney-runkle mentioned this pull request Nov 7, 2025
1 task
@sydney-runkle
Copy link
Collaborator Author

Closing since out of date, but tracking here: #33880

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants